<!DOCTYPE html>
<html lang="en" dir="ltr" class="client-nojs">
<head>
<title>Constant expressions - cppreference.com</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-css:7:472787eddcf4605d11de8c7ef047234f */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/constant_expression","wgTitle":"cpp/language/constant expression","wgCurRevisionId":141458,"wgArticleId":12830,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with unreviewed CWG DR marker","Todo with reason","Pages with unreviewed LWG DR marker"],"wgBreakFrames":false,"wgPageContentLanguage":"en","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgMonthNamesShort":["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"wgRelevantPageName":"cpp/language/constant_expression","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"en","language":"en","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-ColiruCompiler":1,"gadget-MathJax":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-js:7:9f05c6caceb9bb1a482b6cebd4c5a330 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_constant_expression skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">Constant expressions</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">From cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../compiler_support.html" title="cpp/compiler support"> Compiler support</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> Freestanding and hosted</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language"> Language</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header"> Standard library headers</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req"> Named requirements </a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../feature_test.html" title="cpp/feature test"> Feature test macros </a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#Language_support" title="cpp/utility"> Language support library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts"> Concepts library</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../meta.html" title="cpp/meta"> Metaprogramming library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error"> Diagnostics library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility"> General utilities library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string"> Strings library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container"> Containers library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator"> Iterators library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> Ranges library</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm"> Algorithms library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric"> Numerics library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale"> Localizations library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io"> Input/output library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem"> Filesystem library</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex"> Regular expressions library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread"> Concurrency support library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental"> Technical specifications</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../symbol_index.html" title="cpp/symbol index"> Symbols index</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../links/libs.html" title="cpp/links/libs"> External libraries</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language"> C++ language</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h1"><td colspan="5"> General topics</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="../preprocessor.html" title="cpp/preprocessor"> Preprocessor</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../comments.html" title="cpp/comment"> Comments</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="../keywords.html" title="cpp/keyword"> Keywords</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="escape.html" title="cpp/language/escape"> Escape sequences</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> <a href="statements.html" title="cpp/language/statements"> Flow control</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Conditional execution statements</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="if.html" title="cpp/language/if"><tt>if</tt></a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="switch.html" title="cpp/language/switch"><tt>switch</tt></a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Iteration statements (loops)</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="for.html" title="cpp/language/for"><tt>for</tt></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="range-for.html" title="cpp/language/range-for"> range-<code>for</code></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="while.html" title="cpp/language/while"><tt>while</tt></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="do.html" title="cpp/language/do"> <code>do-while</code></a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Jump statements </td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="continue.html" title="cpp/language/continue"><tt>continue</tt></a> - <a href="break.html" title="cpp/language/break"><tt>break</tt></a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="goto.html" title="cpp/language/goto"><tt>goto</tt></a> - <a href="return.html" title="cpp/language/return"><tt>return</tt></a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> <a href="functions.html" title="cpp/language/functions"> Functions</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> Function declaration</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lambda.html" title="cpp/language/lambda"> Lambda function expression</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="inline.html" title="cpp/language/inline"> <code>inline</code> specifier</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="except_spec.html" title="cpp/language/except spec"> Dynamic exception specifications</a> <span class="t-mark-rev t-until-cxx20">(until C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="noexcept_spec.html" title="cpp/language/noexcept spec"> <code>noexcept</code> specifier</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h1"><td colspan="5"> Exceptions</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="throw.html" title="cpp/language/throw"> <code>throw</code>-expression</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="try_catch.html" title="cpp/language/try catch"> <code>try</code>-<code>catch</code> block</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> Namespaces</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="namespace.html" title="cpp/language/namespace"> Namespace declaration</a>  </td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="namespace_alias.html" title="cpp/language/namespace alias"> Namespace aliases</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> Types</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="types.html" title="cpp/language/types"> Fundamental types</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="enum_class.html" title="cpp/language/enum"> Enumeration types</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> Function types</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="class.html" title="cpp/language/class"> Class/struct types</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="union.html" title="cpp/language/union"> Union types</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Specifiers</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="decltype.html" title="cpp/language/decltype"><tt>decltype</tt></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="auto.html" title="cpp/language/auto"><tt>auto</tt></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="alignas.html" title="cpp/language/alignas"><tt>alignas</tt></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="cv.html" title="cpp/language/cv"> <code>const</code>/<code>volatile</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="constexpr.html" title="cpp/language/constexpr"><tt>constexpr</tt></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> Storage duration specifiers</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="initialization.html" title="cpp/language/initialization"> Initialization</a></td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="default_initialization.html" title="cpp/language/default initialization"> Default initialization</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="value_initialization.html" title="cpp/language/value initialization"> Value initialization</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="zero_initialization.html" title="cpp/language/zero initialization"> Zero initialization</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="copy_initialization.html" title="cpp/language/copy initialization"> Copy initialization</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="direct_initialization.html" title="cpp/language/direct initialization"> Direct initialization</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="aggregate_initialization.html" title="cpp/language/aggregate initialization"> Aggregate initialization</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="list_initialization.html" title="cpp/language/list initialization"> List initialization</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="constant_initialization.html" title="cpp/language/constant initialization"> Constant initialization</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reference_initialization.html" title="cpp/language/reference initialization"> Reference initialization</a></td></tr>
</table></div></td></tr>
</table></div>
</div>
<div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h1"><td colspan="5"> <a href="expressions.html" title="cpp/language/expressions"> Expressions</a></td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="value_category.html" title="cpp/language/value category"> Value categories</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="eval_order.html" title="cpp/language/eval order"> Order of evaluation</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="operators.html" title="cpp/language/operators"> Operators</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_precedence.html" title="cpp/language/operator precedence"> Operator precedence</a></td></tr>
</table></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_alternative.html" title="cpp/language/operator alternative"> Alternative representations</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="expressions.html#Literals" title="cpp/language/expressions"> Literals</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bool_literal.html" title="cpp/language/bool literal"> Boolean</a> - <a href="integer_literal.html" title="cpp/language/integer literal"> Integer</a> - <a href="floating_literal.html" title="cpp/language/floating literal"> Floating-point</a></td></tr>   
<tr class="t-nv"><td colspan="5"> <a href="character_literal.html" title="cpp/language/character literal"> Character</a> - <a href="string_literal.html" title="cpp/language/string literal"> String</a> - <a href="nullptr.html" title="cpp/language/nullptr"><tt>nullptr</tt></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr> 
<tr class="t-nv"><td colspan="5"> <a href="user_literal.html" title="cpp/language/user literal"> User-defined</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h1"><td colspan="5"> Utilities</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="cpp/language/attributes"> Attributes</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Types</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="typedef.html" title="cpp/language/typedef"> <code>typedef</code> declaration</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="type_alias.html" title="cpp/language/type alias"> Type alias declaration</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Casts</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="implicit_cast.html" title="cpp/language/implicit conversion"> Implicit conversions</a> - <a href="explicit_cast.html" title="cpp/language/explicit cast"> Explicit conversions</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_cast.html" title="cpp/language/static cast"><tt>static_cast</tt></a> - <a href="dynamic_cast.html" title="cpp/language/dynamic cast"><tt>dynamic_cast</tt></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="const_cast.html" title="cpp/language/const cast"><tt>const_cast</tt></a> - <a href="reinterpret_cast.html" title="cpp/language/reinterpret cast"><tt>reinterpret_cast</tt></a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Memory allocation</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="new.html" title="cpp/language/new"> <code>new</code> expression</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="delete.html" title="cpp/language/delete"> <code>delete</code> expression</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> <a href="classes.html" title="cpp/language/classes"> Classes</a></td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="class.html" title="cpp/language/class"> Class declaration</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="initializer_list.html" title="cpp/language/constructor"> Constructors</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="this.html" title="cpp/language/this"> <code>this</code> pointer</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="access.html" title="cpp/language/access"> Access specifiers</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="friend.html" title="cpp/language/friend"> <code>friend</code> specifier</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Class-specific function properties</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="virtual.html" title="cpp/language/virtual"> Virtual function</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="override.html" title="cpp/language/override"> <code>override</code> specifier</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="final.html" title="cpp/language/final"> <code>final</code> specifier</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="explicit.html" title="cpp/language/explicit"><tt>explicit</tt></a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static.html" title="cpp/language/static"><tt>static</tt></a> </td></tr>
</table></div></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Special member functions</td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="default_constructor.html" title="cpp/language/default constructor"> Default constructor</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="copy_constructor.html" title="cpp/language/copy constructor"> Copy constructor</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="move_constructor.html" title="cpp/language/move constructor"> Move constructor</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="as_operator.html" title="cpp/language/as operator" class="mw-redirect"> Copy assignment</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="move_operator.html" title="cpp/language/move operator" class="mw-redirect"> Move assignment</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="destructor.html" title="cpp/language/destructor"> Destructor</a></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"><a href="templates.html" title="cpp/language/templates"> Templates</a> </td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> Class template </a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> Function template </a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> Template specialization</a></td></tr>   
<tr class="t-nv"><td colspan="5"> <a href="parameter_pack.html" title="cpp/language/parameter pack"> Parameter packs</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
</table></div></td></tr>
<tr class="t-nv-h1"><td colspan="5"> Miscellaneous </td></tr>
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="asm.html" title="cpp/language/asm"> Inline assembly</a></td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="history.html" title="cpp/language/history"> History of C++</a></td></tr>
</table></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="expressions.html" title="cpp/language/expressions"> Expressions</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> General </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="value_category.html" title="cpp/language/value category"> value categories</a> (lvalue, rvalue, xvalue)</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="eval_order.html" title="cpp/language/eval order"> order of evaluation</a> (sequence points)</td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> constant expressions</strong></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="expressions.html#Unevaluated_expressions" title="cpp/language/expressions"> unevaluated expressions</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="expressions.html#Primary_expressions" title="cpp/language/expressions"> primary expressions</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lambda.html" title="cpp/language/lambda">lambda-expression</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Literals </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="integer_literal.html" title="cpp/language/integer literal">integer literals</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="floating_literal.html" title="cpp/language/floating literal">floating-point literals</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bool_literal.html" title="cpp/language/bool literal">boolean literals</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="character_literal.html" title="cpp/language/character literal">character literals</a> including <a href="escape.html" title="cpp/language/escape">escape sequences</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="string_literal.html" title="cpp/language/string literal">string literals</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="nullptr.html" title="cpp/language/nullptr">null pointer literal</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="user_literal.html" title="cpp/language/user literal">user-defined literal</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Operators </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_assignment.html" title="cpp/language/operator assignment"> Assignment operators</a>: 
<code>a=b</code>, <code>a+=b</code>, <code>a-=b</code>, <code>a*=b</code>, <code>a/=b</code>, <code>a%=b</code>, <code>a&amp;=b</code>, <code>a|=b</code>, <code>a^=b</code>, <code>a&lt;&lt;=b</code>, <code>a&gt;&gt;=b</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_incdec.html" title="cpp/language/operator incdec"> Increment and decrement</a>: <code>++a</code>, <code>--a</code>, <code>a++</code>, <code>a--</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_arithmetic.html" title="cpp/language/operator arithmetic"> Arithmetic operators</a>:
<code>+a</code>, <code>-a</code>, <code>a+b</code>, <code>a-b</code>, <code>a*b</code>, <code>a/b</code>, <code>a%b</code>, <code>~a</code>, <code>a&amp;b</code>, <code>a|b</code>, <code>a^b</code>, <code>a&lt;&lt;b</code>, <code>a&gt;&gt;b</code> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_logical.html" title="cpp/language/operator logical"> Logical operators</a>: <code>a||b</code>, <code>a&amp;&amp;b</code>, <code>!a</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_comparison.html" title="cpp/language/operator comparison"> Comparison operators</a>: <code>a==b</code>, <code>a!=b</code>, <code>a&lt;b</code>, <code>a&gt;b</code>, <code>a&lt;=b</code>, <code>a&gt;=b</code>, <code>a&lt;=&gt;b</code><span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_member_access.html" title="cpp/language/operator member access"> Member access operators</a>: <code>a[b]</code>, <code>*a</code>, <code>&amp;a</code>, <code>a-&gt;b</code>, <code>a.b</code>, <code>a-&gt;*b</code>, <code>a.*b</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_other.html" title="cpp/language/operator other"> Other operators</a>: <code>a(...)</code>, <code>a,b</code>, <code>a?b:c</code> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="new.html" title="cpp/language/new">new-expression</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="delete.html" title="cpp/language/delete">delete-expression</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="throw.html" title="cpp/language/throw">throw-expression</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="alignof.html" title="cpp/language/alignof"><code>alignof</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sizeof.html" title="cpp/language/sizeof"><code>sizeof</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sizeof....html" title="cpp/language/sizeof..."><code>sizeof...</code></a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="typeid.html" title="cpp/language/typeid"><code>typeid</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="noexcept.html" title="cpp/language/noexcept"><code>noexcept</code></a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="fold.html" title="cpp/language/fold">Fold expression</a><span class="t-mark-rev t-since-cxx17">(C++17)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_alternative.html" title="cpp/language/operator alternative">Alternative representations of operators</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_precedence.html" title="cpp/language/operator precedence">Precedence and associativity</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operators.html" title="cpp/language/operators">Operator overloading</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="default_comparisons.html" title="cpp/language/default comparisons">Default comparisons</a><span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Conversions </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="implicit_cast.html" title="cpp/language/implicit conversion">Implicit conversions</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="const_cast.html" title="cpp/language/const cast"><code>const_cast</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_cast.html" title="cpp/language/static cast"><code>static_cast</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reinterpret_cast.html" title="cpp/language/reinterpret cast"><code>reinterpret_cast</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="dynamic_cast.html" title="cpp/language/dynamic cast"><code>dynamic_cast</code></a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit_cast.html" title="cpp/language/explicit cast">Explicit conversions</a> <code>(T)a</code>, <code>T(a)</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="cast_operator.html" title="cpp/language/cast operator">User-defined conversion</a></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>Defines an <a href="expressions.html" title="cpp/language/expressions">expression</a> that can be evaluated at compile time.
</p><p>Such expressions can be used as non-type template arguments, array sizes, and in other contexts that require constant expressions, e.g.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
<a href="../container/array.html"><span class="kw1271">std::<span class="me2">array</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span>, n<span class="sy1">&gt;</span> a1<span class="sy4">;</span> <span class="co1">// error: n is not a constant expression</span>
<span class="kw4">const</span> <span class="kw4">int</span> cn <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
<a href="../container/array.html"><span class="kw1271">std::<span class="me2">array</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span>, cn<span class="sy1">&gt;</span> a2<span class="sy4">;</span> <span class="co1">// OK: cn is a constant expression</span></pre></div></div>
<h3><span class="mw-headline" id="Core_constant_expressions">Core constant expressions</span></h3>
<p>A <i>core constant expression</i> is any expression whose evaluation <i>would not</i> evaluate any one of the following:
</p>
<ol>
<li> the <a href="this.html" title="cpp/language/this"><code>this</code></a> pointer, except in a <code>constexpr</code> function that is being evaluated as part of the expression
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx23"> <span><span class="t-mark-rev t-since-cxx23">(since C++23)</span> a control flow that passes through a declaration of a variable with static or thread <a href="storage_duration.html" title="cpp/language/storage duration">storage duration</a></span>
</li><li> a function call expression that calls a function (or a constructor) that is not declared <a href="constexpr.html" title="cpp/language/constexpr">constexpr</a> 
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">constexpr</span> <span class="kw4">int</span> n <span class="sy1">=</span> <a href="../types/numeric_limits.html"><span class="kw350">std::<span class="me2">numeric_limits</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">max</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK: max() is constexpr</span>
<span class="kw4">constexpr</span> <span class="kw4">int</span> m <span class="sy1">=</span> <a href="../chrono/c/time.html"><span class="kw1016">std::<span class="me2">time</span></span></a><span class="br0">(</span>nullptr<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// Error: std::time() is not constexpr</span></pre></div></div>
</li><li> a function call to a <code>constexpr</code> function which is declared, but not defined
</li><li>  a function call to a <code>constexpr</code> function/constructor template instantiation where the instantiation fails to satisfy <a href="constexpr.html" title="cpp/language/constexpr">constexpr function/constructor</a> requirements.
</li><li> a function call to a constexpr virtual function, invoked on an object not <a href="constant_expression.html#Usable_in_constant_expressions">usable in constant expressions</a> and whose lifetime began outside this expression.
</li><li> an expression that would exceed the implementation-defined limits
</li><li> an expression whose evaluation leads to any form of core language undefined behavior (including signed integer overflow, division by zero, pointer arithmetic outside array bounds, etc). Whether standard library undefined behavior is detected is unspecified.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">constexpr</span> <span class="kw4">double</span> d1 <span class="sy1">=</span> <span class="nu16">2.0</span><span class="sy2">/</span><span class="nu16">1.0</span><span class="sy4">;</span> <span class="co1">// OK</span>
<span class="kw4">constexpr</span> <span class="kw4">double</span> d2 <span class="sy1">=</span> <span class="nu16">2.0</span><span class="sy2">/</span><span class="nu16">0.0</span><span class="sy4">;</span> <span class="co1">// Error: not defined</span>
<span class="kw4">constexpr</span> <span class="kw4">int</span> n <span class="sy1">=</span> <a href="../types/numeric_limits.html"><span class="kw350">std::<span class="me2">numeric_limits</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">max</span><span class="br0">(</span><span class="br0">)</span> <span class="sy2">+</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// Error: overflow</span>
<span class="kw4">int</span> x, y, z<span class="br0">[</span><span class="nu0">30</span><span class="br0">]</span><span class="sy4">;</span>
<span class="kw4">constexpr</span> <span class="kw4">auto</span> e1 <span class="sy1">=</span> <span class="sy3">&amp;</span>y <span class="sy2">-</span> <span class="sy3">&amp;</span>x<span class="sy4">;</span> <span class="co1">// Error: undefined</span>
<span class="kw4">constexpr</span> <span class="kw4">auto</span> e2 <span class="sy1">=</span> <span class="sy3">&amp;</span>z<span class="br0">[</span><span class="nu0">20</span><span class="br0">]</span> <span class="sy2">-</span> <span class="sy3">&amp;</span>z<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// OK</span>
<span class="kw4">constexpr</span> <a href="../utility/bitset.html"><span class="kw1032">std::<span class="me2">bitset</span></span></a><span class="sy1">&lt;</span><span class="nu0">2</span><span class="sy1">&gt;</span> a<span class="sy4">;</span> 
<span class="kw4">constexpr</span> <span class="kw4">bool</span> b <span class="sy1">=</span> a<span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// UB, but unspecified if detected</span></pre></div></div>
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx17"> <span><span class="t-mark-rev t-until-cxx17">(until C++17)</span> a <a href="lambda.html" title="cpp/language/lambda">lambda expression</a></span>
</li><li> an lvalue-to-rvalue <a href="implicit_cast.html" title="cpp/language/implicit conversion">implicit conversion</a> unless....
<ol type="a">
<li> applied to a non-volatile glvalue that designates an object that is <a href="constant_expression.html#Usable_in_constant_expressions">usable in constant expressions</a>,  
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">const</span> <a href="../types/size_t.html"><span class="kw106">std::<span class="me2">size_t</span></span></a> tabsize <span class="sy1">=</span> <span class="nu0">50</span><span class="sy4">;</span>
    <span class="kw4">int</span> tab<span class="br0">[</span>tabsize<span class="br0">]</span><span class="sy4">;</span> <span class="co1">// OK: tabsize is a constant expression</span>
                      <span class="co1">// because tabsize is usable in constant expressions</span>
                      <span class="co1">// because it has const-qualified integral type, and</span>
                      <span class="co1">// its initializer is a constant initializer</span>
 
    <a href="../types/size_t.html"><span class="kw106">std::<span class="me2">size_t</span></span></a> n <span class="sy1">=</span> <span class="nu0">50</span><span class="sy4">;</span>
    <span class="kw4">const</span> <a href="../types/size_t.html"><span class="kw106">std::<span class="me2">size_t</span></span></a> sz <span class="sy1">=</span> n<span class="sy4">;</span>
    <span class="kw4">int</span> tab2<span class="br0">[</span>sz<span class="br0">]</span><span class="sy4">;</span> <span class="co1">// error: sz is not a constant expression</span>
                  <span class="co1">// because sz is not usable in constant expressions</span>
                  <span class="co1">// because its initializer was not a constant initializer</span>
<span class="br0">}</span></pre></div></div>
</li><li> or applied to a non-volatile glvalue of literal type that refers to a non-volatile object whose lifetime began within the evaluation of this expression
</li></ol>
</li><li> an lvalue-to-rvalue <a href="implicit_cast.html" title="cpp/language/implicit conversion">implicit conversion</a> or modification applied to a non-active member of a <a href="union.html" title="cpp/language/union">union</a> or its subobject (even if it shares a common initial sequence with the active member)
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span> an lvalue-to-rvalue implicit conversion applied to an object with an <a href="default_initialization.html" title="cpp/language/default initialization">indeterminate value</a></span>
</li><li>  an invocation of an implicitly-defined copy/move constructor or copy/move assignment operator for a union whose active member (if any) is mutable, unless the lifetime of the union object began within the evaluation of this expression
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx17 t-until-cxx20"> <span><span class="t-mark-rev t-since-cxx17">(since C++17)</span> <span class="t-mark-rev t-until-cxx20">(until C++20)</span> an assignment expression or invocation of an overloaded assignment operator that would change the active member of a union</span>
</li><li> an <span class="t-spar">id-expression</span> referring to a variable or a data member of reference type, unless the reference is <a href="constant_expression.html#Usable_in_constant_expressions">usable in constant expressions</a> or its lifetime began within the evaluation of this expression
</li><li> conversion from <span class="t-c"><i>cv</i> <span class="mw-geshi cpp source-cpp"><span class="kw4">void</span><span class="sy2">*</span></span></span> to any pointer-to-object type
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx20"> <span><span class="t-mark-rev t-until-cxx20">(until C++20)</span> <a href="dynamic_cast.html" title="cpp/language/dynamic cast"><code>dynamic_cast</code></a></span>
</li><li> <a href="reinterpret_cast.html" title="cpp/language/reinterpret cast"><code>reinterpret_cast</code></a>
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx20"> <span><span class="t-mark-rev t-until-cxx20">(until C++20)</span> pseudo-destructor call</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx14"> <span><span class="t-mark-rev t-until-cxx14">(until C++14)</span> an increment or a decrement operator</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx14"> <div>
<p><span class="t-mark-rev t-since-cxx14">(since C++14)</span> modification of an object, unless the object has non-volatile literal type and its lifetime began within the evaluation of the expression
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">constexpr</span> <span class="kw4">int</span> incr<span class="br0">(</span><span class="kw4">int</span><span class="sy3">&amp;</span> n<span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw1">return</span> <span class="sy2">++</span>n<span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">constexpr</span> <span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">int</span> k<span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">constexpr</span> <span class="kw4">int</span> x <span class="sy1">=</span> incr<span class="br0">(</span>k<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// error: incr(k) is not a core constant</span>
                               <span class="co1">// expression because lifetime of k</span>
                               <span class="co1">// began outside the expression incr(k)</span>
    <span class="kw1">return</span> x<span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">constexpr</span> <span class="kw4">int</span> h<span class="br0">(</span><span class="kw4">int</span> k<span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">int</span> x <span class="sy1">=</span> incr<span class="br0">(</span>k<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK: x is not required to be initialized</span>
                     <span class="co1">// with a core constant expression</span>
    <span class="kw1">return</span> x<span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">constexpr</span> <span class="kw4">int</span> y <span class="sy1">=</span> h<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK: initializes y with the value 2</span>
                        <span class="co1">// h(1) is a core constant expression because</span>
                        <span class="co1">// the lifetime of k begins inside the expression h(1)</span></pre></div></div>
</div>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span> a destructor call or pseudo destructor call for an object whose lifetime did not begin within the evaluation of this expression</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx20"> <span><span class="t-mark-rev t-until-cxx20">(until C++20)</span> a <a href="typeid.html" title="cpp/language/typeid"><code>typeid</code></a> expression applied to a glvalue of polymorphic type</span>
</li><li> a <a href="new.html" title="cpp/language/new">new-expression</a><span class="t-rev-inl t-since-cxx20"><span>, unless the selected <a href="../memory/new/operator_new.html" title="cpp/memory/new/operator new">allocation function</a> is a replaceable global allocation function and the allocated storage is deallocated within the evaluation of this expression</span> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span></span></span>
</li><li> a <a href="delete.html" title="cpp/language/delete">delete-expression</a><span class="t-rev-inl t-since-cxx20"><span>, unless it deallocates a region of storage allocated within the evaluation of this expession</span> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span></span></span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span> a call to <span class="t-lc"><a href="../memory/allocator/allocate.html" title="cpp/memory/allocator/allocate">std::allocator&lt;T&gt;::allocate</a></span>, unless the allocated storage is deallocated within the evaluation of this expression</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span> a call to <span class="t-lc"><a href="../memory/allocator/deallocate.html" title="cpp/memory/allocator/deallocate">std::allocator&lt;T&gt;::deallocate</a></span>, unless it deallocates a region of storage allocated within the evaluation of this expession</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span> an <a href="coroutines.html#co_await" title="cpp/language/coroutines">await-expression</a> or a <a href="coroutines.html#co_yield" title="cpp/language/coroutines">yield-expression</a></span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span> a <a href="operator_comparison.html#Three-way_comparison" title="cpp/language/operator comparison">three-way comparison</a> when the result is unspecified</span>
</li><li> an equality or relational operator when the result is unspecified
</li><li class="t-rev-inl t-rev-inl-noborder t-until-cxx14"> <span><span class="t-mark-rev t-until-cxx14">(until C++14)</span> an assignment or a compound assignment operator</span>
</li><li> a throw expression
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span> an <a href="asm.html" title="cpp/language/asm">asm-declaration</a></span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx14"> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span> an invocation of the <span class="t-lc"><a href="../utility/variadic/va_arg.html" title="cpp/utility/variadic/va arg">va_arg</a></span> macro, whether an invocation of the <span class="t-lc"><a href="../utility/variadic/va_start.html" title="cpp/utility/variadic/va start">va_start</a></span> macro can be evaluated is unspecified</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx23"> <span><span class="t-mark-rev t-since-cxx23">(since C++23)</span> a <a href="goto.html" title="cpp/language/goto"><code>goto</code></a> statement</span>
</li><li class="t-rev-inl t-rev-inl-noborder t-since-cxx20"> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span> a <a href="dynamic_cast.html" title="cpp/language/dynamic cast"><code>dynamic_cast</code></a> or <a href="typeid.html" title="cpp/language/typeid"><code>typeid</code></a> expression that would throw an exception</span>
</li><li> inside a lambda-expression, a reference to <code>this</code> or to a variable defined outside that lambda, if that reference would be an odr-use 
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">const</span> <span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
 
    <span class="kw4">constexpr</span> <span class="kw4">int</span> j <span class="sy1">=</span> <span class="sy2">*</span><span class="sy3">&amp;</span>n<span class="sy4">;</span> <span class="co1">// OK: outside of a lambda-expression</span>
 
    <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span>
    <span class="br0">{</span>
        <span class="kw4">constexpr</span> <span class="kw4">int</span> i <span class="sy1">=</span> n<span class="sy4">;</span>   <span class="co1">// OK: 'n' is not odr-used and not captured here.</span>
        <span class="kw4">constexpr</span> <span class="kw4">int</span> j <span class="sy1">=</span> <span class="sy2">*</span><span class="sy3">&amp;</span>n<span class="sy4">;</span> <span class="co1">// Ill-formed: '&amp;n' would be an odr-use of 'n'.</span>
    <span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<p>note that if the ODR-use takes place in a function call to a closure, it does not refer to <code>this</code> or to an enclosing variable, since it accesses a closure's data member instead
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// OK: 'v' &amp; 'm' are odr-used but do not occur in a constant-expression</span>
<span class="co1">// within the nested lambda</span>
<span class="kw4">auto</span> monad <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> v<span class="br0">)</span><span class="br0">{</span> <span class="kw1">return</span> <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">{</span> <span class="kw1">return</span> v<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">auto</span> bind <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> m<span class="br0">)</span><span class="br0">{</span> <span class="kw1">return</span> <span class="br0">[</span><span class="sy1">=</span><span class="br0">]</span><span class="br0">(</span><span class="kw4">auto</span> fvm<span class="br0">)</span><span class="br0">{</span> <span class="kw1">return</span> fvm<span class="br0">(</span>m<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
 
<span class="co1">// OK to have captures to automatic objects created during constant expression evaluation.</span>
static_assert<span class="br0">(</span>bind<span class="br0">(</span>monad<span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">)</span><span class="br0">(</span>monad<span class="br0">)</span><span class="br0">(</span><span class="br0">)</span> <span class="sy1">==</span> monad<span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx17">(since C++17)</span></td></tr>
</table>
</li></ol>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">This section is incomplete<br>Reason: needs more mini-examples and less standardese </td></tr></table>
<p>Note: Just being a <i>core constant expression</i> does not have any direct semantic meaning: an expression has to be one of the subsets of constant expressions (see below) to be used in certain contexts.
</p>
<h3><span class="mw-headline" id="Constant_expression">Constant expression</span></h3>
<p>A <i>constant expression</i> is either
</p>
<ul><li> <span class="t-rev-inl t-until-cxx14"><span>an lvalue</span> <span><span class="t-mark-rev t-until-cxx14">(until C++14)</span></span></span><span class="t-rev-inl t-since-cxx14"><span>a glvalue</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> <i>core constant expression</i> that refers to
</li></ul>
<dl><dd><ul><li> an object with static storage duration that is not a temporary, or
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<dl><dd><ul><li> an object with static storage duration that is a temporary, but whose value satisfies the constraints for prvalues below, or
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<dl><dd><ul><li> a <span class="t-rev-inl t-since-cxx20"><span>non-<a href="consteval.html" title="cpp/language/consteval">immediate</a></span> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span></span></span> function
</li></ul>
</dd></dl>
<ul><li> a prvalue <i>core constant expression</i> whose value satisfies the following constraints:
</li></ul>
<dl><dd><ul><li> if the value is an object of class type, each non-static data member of reference type refers to an entity that satisfies the constraints for <span class="t-rev-inl t-until-cxx14"><span>lvalues</span> <span><span class="t-mark-rev t-until-cxx14">(until C++14)</span></span></span><span class="t-rev-inl t-since-cxx14"><span>glvalues</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> above
</li><li> if the value is of pointer type, it holds
</li></ul>
<dl><dd><ul><li> address of an object with static storage duration
</li><li> address past the end of an object with static storage duration
</li><li> address of a <span class="t-rev-inl t-since-cxx20"><span>non-<a href="consteval.html" title="cpp/language/consteval">immediate</a></span> <span><span class="t-mark-rev t-since-cxx20">(since C++20)</span></span></span> function
</li><li> a null pointer value
</li></ul>
</dd></dl>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<dl><dd><ul><li> if the value is of pointer-to-member-function type, it does not designate an <a href="consteval.html" title="cpp/language/consteval">immediate function</a>
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx20">(since C++20)</span></td></tr>
</table>
<dl><dd><ul><li> if the value is an object of class or array type, each subobject satisfies these constraints for values
</li></ul>
</dd></dl>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">This section is incomplete<br>Reason:  list of contexts that require constant expressions that aren't integral/converted? </td></tr></table>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> test<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> a <span class="sy1">=</span> <a href="../numeric/random/random_device.html"><span class="kw1489">std::<span class="me2">random_device</span></span></a><span class="br0">{</span><span class="br0">}</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">constexpr</span> <span class="kw4">const</span> <span class="kw4">int</span><span class="sy3">&amp;</span> ra <span class="sy1">=</span> a<span class="sy4">;</span> <span class="co1">// OK: a is a glvalue constant expression</span>
    <span class="kw4">constexpr</span> <span class="kw4">int</span> ia <span class="sy1">=</span> a<span class="sy4">;</span> <span class="co1">// Error: a is not a prvalue constant expression</span>
 
    <span class="kw4">const</span> <span class="kw4">int</span> b <span class="sy1">=</span> <span class="nu0">42</span><span class="sy4">;</span>
    <span class="kw4">constexpr</span> <span class="kw4">const</span> <span class="kw4">int</span><span class="sy3">&amp;</span> rb <span class="sy1">=</span> b<span class="sy4">;</span> <span class="co1">// Error: b is not a glvalue constant expression</span>
    <span class="kw4">constexpr</span> <span class="kw4">int</span> ib <span class="sy1">=</span> b<span class="sy4">;</span> <span class="co1">// OK: b is a prvalue constant expression</span>
<span class="br0">}</span></pre></div></div>
<h4><span class="mw-headline" id="Integral_constant_expression">Integral constant expression</span></h4>
<p><i>Integral constant expression</i> is an expression of integral or unscoped enumeration type implicitly converted to a prvalue, where the converted expression is a <i>core constant expression</i>. If an expression of class type is used where an integral constant expression is expected, the expression is <a href="implicit_cast.html" title="cpp/language/implicit cast" class="mw-redirect">contextually implicitly converted</a> to an integral or unscoped enumeration type. 
</p><p>The following contexts require an <i>integral constant expression</i>:
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx14"><td>
<ul><li> <a href="array.html" title="cpp/language/array">array bounds</a>
</li><li> the dimensions in <a href="new.html" title="cpp/language/new">new-expressions</a> other than the first
</li></ul>
</td>
<td><span class="t-mark-rev t-until-cxx14">(until C++14)</span></td></tr>
</table>
<ul><li> <a href="bitfield.html" title="cpp/language/bit field">bit-field lengths</a>
</li><li> enumeration initializers when the underlying type is not fixed
</li><li> <a href="alignas.html" title="cpp/language/alignas">alignments</a>.
</li></ul>
<h4><span class="mw-headline" id="Converted_constant_expression">Converted constant expression</span></h4>
<p>A <i>converted constant expression</i> of type <code>T</code> is an expression <a href="implicit_cast.html" title="cpp/language/implicit cast" class="mw-redirect">implicitly converted</a> to type T, where the converted expression is a constant expression, and the implicit conversion sequence contains only:
</p>
<dl><dd><ul><li> constexpr user-defined conversions (so a class can be used where integral type is expected)
</li><li> lvalue-to-rvalue conversions
</li><li> integral promotions
</li><li> non-narrowing integral conversions
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<dl><dd><ul><li> array-to-pointer conversions
</li><li> function-to-pointer conversions
</li><li> function pointer conversions (pointer to noexcept function to pointer to function)
</li><li> qualification conversions
</li><li> null pointer conversions from <span class="t-lc"><a href="../types/nullptr_t.html" title="cpp/types/nullptr t">std::nullptr_t</a></span>
</li><li> null member pointer conversions from <span class="t-lc"><a href="../types/nullptr_t.html" title="cpp/types/nullptr t">std::nullptr_t</a></span>
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx17">(since C++17)</span></td></tr>
</table>
<dl><dd><ul><li> And if any <a href="reference_initialization.html" title="cpp/language/reference initialization">reference binding</a> takes place, it is direct binding (not one that constructs a temporary object)
</li></ul>
</dd></dl>
<p>The following contexts require a <i>converted constant expression</i>:
</p>
<ul><li> <a href="switch.html" title="cpp/language/switch">case expressions</a>
</li><li> <a href="enum_class.html" title="cpp/language/enum">enumerator initializers</a> when the underlying type is fixed
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<ul><li> <a href="array.html" title="cpp/language/array">array bounds</a>
</li><li> the dimensions in <a href="new.html" title="cpp/language/new">new-expressions</a> other than the first
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<ul><li> <span class="t-rev-inl t-until-cxx17"><span>integral and enumeration </span> <span><span class="t-mark-rev t-until-cxx17">(until C++17)</span></span></span>non-type <a href="template_parameters.html" title="cpp/language/template parameters">template arguments</a>.
</li></ul>
<p>A <i>contextually converted constant expression of type <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span></span></span></i> is an expression,  <a href="implicit_cast.html#Contextual_conversions" title="cpp/language/implicit cast" class="mw-redirect">contextually converted to <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span></span></span></a>, where the converted expression is a constant expression and the conversion sequence contains only the conversions above.
</p><p>The following contexts require a <i>contextually converted constant expression of type <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span></span></span></i>:
</p>
<ul><li> <a href="noexcept_spec.html" title="cpp/language/noexcept spec"><code>noexcept</code> specifications</a>
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx23"><td>
<ul><li> <a href="static_assert.html" title="cpp/language/static assert"><code>static_assert</code> declarations</a>
</li></ul>
</td>
<td><span class="t-mark-rev t-until-cxx23">(until C++23)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17 t-until-cxx23"><td>
<ul><li> <a href="if.html#Constexpr_if" title="cpp/language/if">constexpr <code>if</code> statements</a>
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx17">(since C++17)</span><br><span class="t-mark-rev t-until-cxx23">(until C++23)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<ul><li> <a href="explicit.html" title="cpp/language/explicit">conditional <code>explicit</code> specifiers</a>
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx20">(since C++20)</span></td></tr>
</table>
<h4><span class="mw-headline" id="Historical_categories">Historical categories</span></h4>
<p>Categories of constant expressions listed below are no longer used in the standard since C++14:
</p>
<ul><li> A <i>literal constant expression</i> is a prvalue <i>core constant expression</i> of non-pointer literal type (after conversions as required by context). A literal constant expression of array or class type requires that each subobject is initialized with a constant expression.
</li></ul>
<ul><li> A <i>reference constant expression</i> is an lvalue core constant expression that designates an object with static storage duration or a function.
</li></ul>
<ul><li> An <i>address constant expression</i> is a prvalue core constant expression (after conversions required by context) of type <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../types/nullptr_t.html"><span class="kw108">std::<span class="me2">nullptr_t</span></span></a></span></span> or of a pointer type, which points to an object with static storage duration, one past the end of an array with static storage duration, to a function, or is a null pointer.
</li></ul>
<h3><span class="mw-headline" id="Usable_in_constant_expressions">Usable in constant expressions</span></h3>
<p>In the list above, a variable is <i>usable in constant expressions</i> at a point <code><b>P</b></code> if 
</p>
<ul><li> the variable is
</li></ul>
<dl><dd><ul><li> a constexpr variable, or
</li><li> it is a <a href="constant_initialization.html" title="cpp/language/constant initialization">constant-initialized</a> variable
</li></ul>
<dl><dd><ul><li> of reference type or
</li><li> of const-qualified integral or enumeration type
</li></ul>
</dd></dl>
</dd></dl>
<ul><li> and the definition of the variable is reachable from <code><b>P</b></code>
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<ul><li> and, if <code><b>P</b></code> is not in the same translation unit as the definition of the variable (that is, the definition is <a href="modules.html" title="cpp/language/modules">imported</a>), the variable is not initialized to point to, or refer to, or have a (possibly recursive) subobject that points to or refers to, a <a href="tu_local.html" title="cpp/language/tu local">translation-unit-local entity</a> that is usable in constant expressions
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx20">(since C++20)</span></td></tr>
</table>
<p>An object or reference is <i>usable in constant expressions</i> if it is
</p>
<ul><li> a variable that is usable in constant expressions, or
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<ul><li> a <a href="template_parameters.html#template_parameter_object" title="cpp/language/template parameters">template parameter object</a>, or
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx20">(since C++20)</span></td></tr>
</table>
<ul><li> a string literal object, or
</li><li> a non-mutable subobject or reference member of any of the above, or
</li><li> a temporary object of non-volatile const-qualified literal type whose lifetime is <a href="reference_initialization.html#Lifetime_of_a_temporary" title="cpp/language/reference initialization">extended</a> to that of a variable that is usable in constant expressions.
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">const</span> <a href="../types/size_t.html"><span class="kw106">std::<span class="me2">size_t</span></span></a> sz <span class="sy1">=</span> <span class="nu0">10</span><span class="sy4">;</span> <span class="co1">// sz is usable in constant expressions</span></pre></div></div>
<h3><span class="mw-headline" id="Manifestly_constant-evaluated_expressions">Manifestly constant-evaluated expressions</span></h3>
<p>The following expressions (including conversions to the destination type) are <i>manifestly constant-evaluated</i>:
</p>
<ul><li> Where a constant expression is grammatically required, including:
</li></ul>
<dl><dd><ul><li> <a href="array.html" title="cpp/language/array">array bounds</a>
</li><li> the dimensions in <a href="new.html" title="cpp/language/new">new-expressions</a> other than the first
</li><li> <a href="bitfield.html" title="cpp/language/bit field">bit-field lengths</a>
</li><li> <a href="enum_class.html" title="cpp/language/enum">enumeration initializers</a>
</li><li> <a href="alignas.html" title="cpp/language/alignas">alignments</a>
</li><li> <a href="switch.html" title="cpp/language/switch"><code>case</code> expressions</a>
</li><li> non-type <a href="template_parameters.html" title="cpp/language/template parameters">template arguments</a>
</li><li> expressions in <a href="noexcept_spec.html" title="cpp/language/noexcept spec"><code>noexcept</code> specifications</a>
</li><li> expressions in <a href="static_assert.html" title="cpp/language/static assert"><code>static_assert</code> declarations</a>
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<dl><dd><ul><li> expressions in <a href="explicit.html" title="cpp/language/explicit">conditional <code>explicit</code> specifiers</a>
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx20">(since C++20)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<ul><li> conditions of <a href="if.html#Constexpr_if" title="cpp/language/if">constexpr if statements</a>
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx17">(since C++17)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<ul><li> <a href="consteval.html" title="cpp/language/consteval">immediate invocations</a>
</li><li> constraint expressions in <a href="constraints.html#Concepts" title="cpp/language/constraints">concept</a> definitions, <a href="constraints.html#Nested_requirements" title="cpp/language/constraints">nested requirements</a>, and <a href="constraints.html#Requires_clauses" title="cpp/language/constraints">requires clauses</a>
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx20">(since C++20)</span></td></tr>
</table>
<ul><li> initializers of variables that are <a href="constant_expression.html#Usable_in_constant_expressions">usable in constant expressions</a>, including:
</li></ul>
<dl><dd><ul><li> initializers of <a href="constexpr.html" title="cpp/language/constexpr">constexpr variables</a>
</li><li> initializers of variables with reference type or const-qualified integral or enumeration type, when the initializers are constant expressions
</li></ul>
</dd></dl>
<ul><li> initializers of static and thread local variables, when all subexpressions of the initializers (including constructor calls and implicit conversions) are constant expressions (that is, when the initializers are <a href="constant_initialization.html" title="cpp/language/constant initialization">constant initializers</a>)
</li></ul>
<p>Note the context of the last two cases also accept non-constant expressions.
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<p>Whether an evaluation occurs in a manifestly constant-evaluated context can be detected by <span class="t-lc"><a href="../types/is_constant_evaluated.html" title="cpp/types/is constant evaluated">std::is_constant_evaluated</a></span><span class="t-rev-inl t-since-cxx23"><span> and <a href="if.html#Consteval_if" title="cpp/language/if"><code>if consteval</code></a></span> <span><span class="t-mark-rev t-since-cxx23">(since C++23)</span></span></span>.
</p><p>To test the last two conditions, compilers may first perform a trial constant evaluation of the initializers. It is not recommended to depend on the result in this case.
</p>
</td>
<td><span class="t-mark-rev t-since-cxx20">(since C++20)</span></td></tr>
</table>
<h3><span class="mw-headline" id="Functions_and_variables_needed_for_constant_evaluation">Functions and variables needed for constant evaluation</span></h3>
<p>Following expressions or conversions are <i>potentially constant evaluated</i>:
</p>
<ul><li> manifestly constant-evaluated expressions
</li><li> potentially-evaluated expressions
</li><li> immediate subexpressions of a braced-init-list (constant evaluation may be necessary to determine whether <a href="list_initialization.html#Narrowing_conversions" title="cpp/language/list initialization">a conversion is narrowing</a>)
</li><li> address-of (unary <code>&amp;</code>) expressions that occur within a <a href="templates.html#Templated_entity" title="cpp/language/templates">templated entity</a> (constant evaluation may be necessary to determine whether such an expression is <a href="dependent_name.html#Value-dependent_expressions" title="cpp/language/dependent name">value-dependent</a>)
</li><li> subexpressions of one of the above that are not a subexpression of a nested <a href="expressions.html#Unevaluated_expressions" title="cpp/language/expressions">unevaluated operand</a>
</li></ul>
<p>A function is <i>needed for constant evaluation</i> if it is a constexpr function and <a href="definition.html#Naming_a_function" title="cpp/language/definition">named by</a> an expression that is potentially constant evaluated.
</p><p>A variable is <i>needed for constant evaluation</i> if it is either a constexpr variable or is of non-volatile const-qualified integral type or of reference type and the <a href="expressions.html#Primary_expressions" title="cpp/language/expressions">id-expression</a> that denotes it is potentially constant evaluated.
</p><p>Definition of a defaulted function and instantiation of a <a href="function_template.html" title="cpp/language/function template">function template</a> specialization <span class="t-rev-inl t-since-cxx14"><span>or <a href="variable_template.html" title="cpp/language/variable template">variable template</a> specialization</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> are triggered if the function <span class="t-rev-inl t-since-cxx14"><span>or variable</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> is needed for constant evaluation.
</p>
<h3><span class="mw-headline" id="Notes">Notes</span></h3>
<p>Implementations are not permitted to declare library functions as <code>constexpr</code> unless the standard says the function is <code>constexpr</code> 
</p><p><a href="copy_elision.html" title="cpp/language/copy elision">Named return value optimization</a> (NRVO) is not permitted in constant expressions, while return value optimization (RVO) is mandatory. 
</p>
<h3><span class="mw-headline" id="Defect_reports">Defect reports</span></h3>
<p>The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> Applied to
</th>
<th> Behavior as published
</th>
<th> Correct behavior
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1293.html">CWG 1293</a>
</td>
<td> C++11
</td>
<td> it was unspecified whether string literals<br>are usable in constant expressions
</td>
<td> they are usable
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1311.html">CWG 1311</a>
</td>
<td> C++11
</td>
<td> volatile glvalues could be used in constant expressions
</td>
<td> prohibited
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1312.html">CWG 1312</a>
</td>
<td> C++11
</td>
<td> <code>reinterpret_cast</code> is prohibited in constant expressions,<br>but casting to and from void* could achieve the same effect
</td>
<td> prohibited conversions<br>from type <span class="t-c"><i>cv</i> <span class="mw-geshi cpp source-cpp"><span class="kw4">void</span><span class="sy2">*</span></span></span> to<br>a pointer-to-object type
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1313.html">CWG 1313</a>
</td>
<td> C++11
</td>
<td> undefined behavior was permitted;<br>all pointer subtraction was prohibited
</td>
<td> UB prohibited; same-array<br>pointer subtraction OK
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1405.html">CWG 1405</a>
</td>
<td> C++11
</td>
<td> for objects that are usable in constant expressions,<br>their mutable subobjects were also usable
</td>
<td> they are not usable
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1454.html">CWG 1454</a>
</td>
<td> C++11
</td>
<td> passing constants through constexpr<br>functions via references was not allowed
</td>
<td> allowed
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1455.html">CWG 1455</a>
</td>
<td> C++11
</td>
<td> converted constant expressions could only be prvalues
</td>
<td> can be lvalues
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1456.html">CWG 1456</a>
</td>
<td> C++11
</td>
<td> an address constant expression could not<br>designate the address one past the end of an array
</td>
<td> allowed
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1535.html">CWG 1535</a>
</td>
<td> C++11
</td>
<td> a <code>typeid</code> expression whose operand is of a<br>polymorphic class type was not a core constant<br>expression even if no runtime check is involved
</td>
<td> the operand constraint<br>is limited to glvalues of<br>polymorphic class types
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1581.html">CWG 1581</a>
</td>
<td> C++11
</td>
<td> functions needed for constant evaluation were<br>not required to be defined or instantiated
</td>
<td> required
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1694.html">CWG 1694</a>
</td>
<td> C++11
</td>
<td> binding the value of a temporary to a static storage<br>duration reference was a constant expression
</td>
<td> it is not a<br>constant expression
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/1952.html">CWG 1952</a>
</td>
<td> C++11
</td>
<td> standard library undefined behaviors<br>were required to be diagnosed
</td>
<td> unspecified whether<br>they are diagnosed
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/2126.html">CWG 2126</a>
</td>
<td> C++11
</td>
<td> constant initialized lifetime-extended temporaries of const-<br>qualified literal types were not usable in constant expressions
</td>
<td> usable
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/2167.html">CWG 2167</a>
</td>
<td> C++11
</td>
<td> non-member references local to an evaluation<br>made the evaluation non-constexpr
</td>
<td> non-member<br>references allowed
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/2299.html">CWG 2299</a>
</td>
<td> C++14
</td>
<td> it was unclear whether macros in <a href="../header/cstdarg.html" title="cpp/header/cstdarg"><tt>&lt;cstdarg&gt;</tt></a><br>can be used in constant evaluation
</td>
<td> <code>va_arg</code> forbidden,<br><code>va_start</code> unspecified
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/2400.html">CWG 2400</a>
</td>
<td> C++11
</td>
<td> invoking a constexpr virtual function on an object not usable<br>in constant expressions and whose lifetime began outside the<br>expression containing the invocation could be a constant expression
</td>
<td> it is not a<br>constant expression
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/2418.html">CWG 2418</a>
</td>
<td> C++11
</td>
<td> it was unspecified which object or reference that<br>are not variables are usable in constant expressions
</td>
<td> specified
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://cplusplus.github.io/CWG/issues/2490.html">CWG 2490</a>
</td>
<td> C++20
</td>
<td> (pseudo) destructor calls lacked<br>restrictions in constant evaluation
</td>
<td> restriction added
</td></tr></table>
<h3><span class="mw-headline" id="See_also">See also</span></h3>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <a href="constexpr.html" title="cpp/language/constexpr"> <code>constexpr</code> specifier</a><span class="t-mark-rev t-since-cxx11">(C++11)</span>
</td>
<td>  specifies that the value of a variable or function can be computed at compile time</td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../types/is_literal_type.html" title="cpp/types/is literal type"> <span class="t-lines"><span>is_literal_type</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-deprecated-cxx17">(deprecated in C++17)</span></span><span><span class="t-mark-rev t-until-cxx20">(removed in C++20)</span></span></span></div></div>
</td>
<td>   checks if a type is a literal type  <br> <span class="t-mark">(class template)</span> </td></tr>

</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 6861/1000000
Preprocessor generated node count: 16992/1000000
Post‐expand include size: 194742/2097152 bytes
Template argument size: 34749/2097152 bytes
Highest expansion depth: 22/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_en_:pcache:idhash:12830-0!*!0!!en!*!* and timestamp 20220728023830 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    Retrieved from "<a href="https://en.cppreference.com/mwiki/index.php?title=cpp/language/constant_expression&amp;oldid=141458">https://en.cppreference.com/mwiki/index.php?title=cpp/language/constant_expression&amp;oldid=141458</a>"                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>Navigation</h5>
            <ul><li><a href="https://en.cppreference.com/w/cpp/language/constant_expression">Online version</a></li><li>Offline version retrieved 2022-07-30 14:05.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> This page was last modified on 27 July 2022, at 19:38.</li>
                                    <li id="footer-info-viewcount">This page has been accessed 375,548 times.</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.ColiruCompiler","ext.gadget.MathJax"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.033 secs. -->
	</body>
<!-- Cached 20220728025327 -->
</html>